cmake_minimum_required(VERSION 3.22)

project(btlu LANGUAGES C CXX Fortran)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

if (NOT DEFINED enable_openmp)
    set(enable_openmp TRUE)
endif ()

find_package(OpenMP)


if(OPENMP_FOUND)
    set(CMAKE_C_FLAGS "${OpenMP_C_FLAGS} ${CMAKE_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
# The following causes problem with cmake/3.20.+
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_C_FLAGS}")
    message("-- OpenMP_EXE_LINKER_FLAGS='${OpenMP_EXE_LINKER_FLAGS}'")
    message("-- CMAKE_EXE_LINKER_FLAGS='${CMAKE_EXE_LINKER_FLAGS}'")
endif()  

# === BLAS Selection ===

# Optionally allow users to set a custom BLAS vendor
set(BLA_VENDOR "" CACHE STRING "BLAS vendor (e.g., Intel10_64lp, OpenBLAS, Generic)")
set(BLAS_LIBRARIES "" CACHE STRING "User-specified BLAS library path(s)")

# Try to find MKL first
if(NOT BLA_VENDOR AND NOT BLAS_LIBRARIES)
  set(BLA_VENDOR "Intel10_64lp")
  find_package(BLAS)
  if(NOT BLAS_FOUND)
    # Fall back to OpenBLAS
    set(BLA_VENDOR "OpenBLAS")
    find_package(BLAS)
  endif()
elseif(BLA_VENDOR OR BLAS_LIBRARIES)
  find_package(BLAS REQUIRED)
else()
  message(FATAL_ERROR "Failed to find a suitable BLAS library (MKL or OpenBLAS).")
endif()

if(BLAS_FOUND)
  message(STATUS "Found BLAS: ${BLAS_LIBRARIES}")
else()
  message(FATAL_ERROR "BLAS not found. Please specify BLAS_LIBRARIES or BLA_VENDOR manually.")
endif()

# === Add subdirectories ===
add_subdirectory(src)
add_subdirectory(test)
